package com.sunlands.com.service;

import com.google.gson.Gson;
import com.sunlands.com.service.po.SendMessageReq;
import com.sunlands.com.service.utils.CommonHttpUtil;
import com.sunlands.com.service.utils.IPUtil;
import com.sunlands.com.service.utils.MD5Util;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yusong on 2018/5/18.
 * 发送消息服务
 */
public class WechatRpcService {


    private static final String BUSINESS_ID = "testBusinessId";

    private static final String MD5_KEY = "testSalt";

    private static final String CONTENT = "Hystrix Test";

    private static final String URL = "http://172.16.100.38:9003/message/inform";


    public static String sendMsg(String email,String content) throws Exception {
        SendMessageReq sendMessageReq = new SendMessageReq();
        sendMessageReq.setBusinessId(BUSINESS_ID);
        List<String> recipients = new ArrayList<>();
        recipients.add(email);
        sendMessageReq.setRecipients(recipients);
        sendMessageReq.setContent(content);
        //sendMessageReq.setRemark(IPUtil.getLocalHost());
        Gson gson = new Gson();
        addSign(sendMessageReq);
        String req = gson.toJson(sendMessageReq);
        String res = null;
        try {
            res = CommonHttpUtil.postJson(URL, req);
        } catch (Exception e) {
            return null;
        }
        return res;
    }

    /*
    *添加签名
    */
    private static void addSign(SendMessageReq sendMessageReq) {
        String source = sendMessageReq.getBusinessId() + sendMessageReq.getContent() + MD5_KEY;
        String sign = MD5Util.md5Encode(source);
        sendMessageReq.setSign(sign);
    }


}